/**
 * Copyright 2008 floop.pl
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package org.eaicompiler.sqlgen.builders;

import java.util.Iterator;
import java.util.SortedMap;

import org.eaicompiler.model.ScriptDefinition;
import org.eaicompiler.model.Spawn;

/**
 * Builder for summons SQL
 * 
 * @author floop.pl
 */
public class SummonsSQLBuilder extends SQLBuilder {
	private StringBuilder							sql;
	private boolean									sqlBuilt			= false;
	private boolean									useDelete;
	private SortedMap<Integer, ScriptDefinition>	creatures;

	public SummonsSQLBuilder(boolean useDelete, SortedMap<Integer, ScriptDefinition> creatures) {
		super();
		this.useDelete = useDelete;
		this.sql = new StringBuilder();
		this.creatures = creatures;
	}

	public String buildSQLForCreatures() {
		if (!sqlBuilt) {
			if (useDelete) {
				boolean firstDelete = true;
				boolean firstElement = true;
				int count = 0;
				Iterator<Integer> it = creatures.keySet().iterator();
				while (it.hasNext()) {
					Integer id = it.next();
					ScriptDefinition script = creatures.get(id);
					if (script.getSpawns().size() > 0) {
						if (firstDelete) {
							sql.append("delete from `eventai_summons` where `id` in\n\t(");
							firstDelete = false;
						}
						Iterator<Spawn> itT = script.getSpawns().iterator();
						while (itT.hasNext()) {
							Spawn t = itT.next();
							if (!firstElement) {
								sql.append(",");
							} else {
								firstElement = false;
							}
							if (count % MAX_DELETE_IN_LINE == MAX_DELETE_IN_LINE-1) {
								sql.append("\n\t");
							}
							sql.append("'");
							sql.append(id.intValue() * 100 + t.getId() - 1);
							sql.append("'");
							count++;
						}
					}
				}
				if(!firstElement)
				{
					sql.append(");\n");
				}
			}
			boolean firstInsert = true;
			boolean firstElement = true;			
			Iterator<Integer> it = creatures.keySet().iterator();
			while (it.hasNext()) {
				Integer id = it.next();
				ScriptDefinition script = creatures.get(id);
				if (script.getSpawns().size() > 0) {
					if (firstInsert) {
						sql.append("insert into `eventai_summons` (`id`, `position_x`, `position_y`, `position_z`,\n\t`orientation`, `spawntimesecs`,\n\t`comment`) values\n");
						firstInsert = false;
					}
					Iterator<Spawn> itT = script.getSpawns().iterator();
					while (itT.hasNext()) {
						Spawn s = itT.next();
						if (!firstElement) {
							sql.append(",\n");
						} else {
							firstElement = false;
						}						
						sql.append("\t('");
						sql.append(id.intValue() * 100 + s.getId() - 1);
						sql.append("',");
						sql.append("\'");
						sql.append(s.getX());
						sql.append("\',");
						sql.append("\'");
						sql.append(s.getY());
						sql.append("\',");
						sql.append("\'");
						sql.append(s.getZ());
						sql.append("\',");
						sql.append("\'");
						sql.append(s.getO());
						sql.append("\',");
						sql.append("\'");
						sql.append(s.getTime());
						sql.append("\',");
						sql.append("\'");
						sql.append(id.intValue());
						sql.append("\'");
						sql.append(")");
					}
				}
			}
			if(!firstElement)
			{
				sql.append(";\n");
			}
			sqlBuilt = true;
		}
		return sql.toString();
	}
	
}
